home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr31 / hades.zip / OPTIMIZE.C < prev    next >
C/C++ Source or Header  |  1992-04-14  |  6KB  |  277 lines

  1.  
  2. /***************
  3. **
  4. **  optimize.c
  5. **  last revised: april 10, 1992
  6. **
  7. **  Optimize optimizes a dictionary by sorting it and removing all
  8. **  multiple entries from it. Furthermore, all entries are shortened
  9. **  to 8 or less characters.
  10. **
  11. **  Written for DESPERATE password-cracker with HADES encryption engine by
  12. **  Remote.
  13. **
  14. **  Copyright (C)1992, Zabkar
  15. **
  16. **  root@waves.hacktic.nl (Zabkar)
  17. **  root@room101.hacktic.nl (Remote)
  18. **
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <alloc.h>
  24.  
  25.  
  26. FILE *infile;
  27. FILE *outfile;
  28.  
  29.  
  30. struct words
  31. {
  32.     char word[9];
  33.     struct words *next;
  34. };
  35.  
  36.  
  37.  
  38. /***************
  39.  haltusage()
  40.  prints correct usage and exits
  41. ****************/
  42.  
  43. void haltusage()
  44. {
  45.   fprintf(stderr,
  46.   "Optimize version 1.00 alpha, Copyright (C)1992 Zabkar\n"\
  47.   "DESPERATE password-cracker 1.0 alpha using HADES engine by Remote.\n\n"\
  48.   "Usage: optimize [-r] [-f fromdict] [-o todict]\n\n"\
  49.   "\t-r: sort in reversed order (descending). default is ascending.\n"\
  50.   "\t-f: read from 'fromdict' instead of stdin\n"\
  51.   "\t-o: write to 'todict' instead of stdout\n\n"\
  52.   "no -f specified: dictfile read from stdin\n"\
  53.   "no -o specified: output written to stdout\n");
  54.   exit(0);
  55. }
  56.  
  57.  
  58. /***************
  59.  freebuf()
  60.  freebuf erases all memory occupied by linked-list begin.
  61. ***************/
  62.  
  63. void free_buf(struct words *begin)
  64. {
  65.     struct words *p;
  66.  
  67.     if (begin)
  68.     {
  69.       p = begin;
  70.       while (p != NULL)
  71.       {
  72.         p = p->next;
  73.         free(begin);
  74.         begin = p;
  75.       }
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. /***************
  82.  haltmemory()
  83.  displays out-of-memory message and stops execution.
  84. ***************/
  85.  
  86. void haltmemory(struct words *begin)
  87. {
  88.     free_buf(begin);
  89.  
  90.     fprintf(stderr,
  91.     "Not enaugh memory to sort entire file, split dictionary and run\n"\
  92.     "optimize again for all parts. Then run merge to merge files.\n");
  93.     exit(0);
  94. }
  95.     
  96.  
  97.  
  98. /***************
  99.  sort()
  100.  sorts wordfile `infile' to linked-list begin. if asc==0 then wordfile
  101.  is sorted in descending order, otherwise in ascending order.
  102. ***************/
  103.  
  104. void *sort(FILE *infile, struct words **begin, char asc)
  105. {
  106.     struct words *p;
  107.     struct words *q;
  108.     struct words *r;
  109.     char s[80];
  110.  
  111.     if (!(*begin = malloc(sizeof(struct words))))
  112.        haltmemory(*begin);
  113.  
  114.     strcpy((*begin)->word, "");
  115.     (*begin)->next = NULL;
  116.  
  117.     while (fscanf(infile, "%s", s) == 1)
  118.     {
  119.         if (strlen(s) > 8) s[8]='\0';
  120.         if (!(q = malloc(sizeof(struct words))))
  121.            haltmemory(*begin);
  122.  
  123.         p = *begin;
  124.  
  125.         if (asc)
  126.           while ((p->next != NULL) && (strcmp(p->next->word,s) < 0))
  127.              p = p->next;
  128.         else
  129.           while ((p->next != NULL) && (strcmp(p->next->word,s) > 0))
  130.              p = p->next;     /* descending */
  131.  
  132.         r = p->next;
  133.         p->next = q;
  134.         strcpy(q->word, s);
  135.         q->next = r;
  136.     }
  137. }
  138.  
  139.  
  140.  
  141. /***************
  142.  to_file()
  143.  writes contents of linked-list begin to file of.
  144. ***************/
  145.  
  146. void to_file(FILE *of, struct words *begin)
  147. {
  148.     struct words *p;
  149.  
  150.     p = begin->next;
  151.     while (p != NULL)
  152.     {
  153.         fprintf(of, "%s\n", p->word);
  154.         p = p->next;
  155.     }
  156. }
  157.  
  158.  
  159. /***************
  160.  remdouble()
  161.  removes double entries from sorted linked list begin.
  162. ***************/
  163.  
  164. void remdouble(struct words *begin)
  165. {
  166.     struct words *p, *q;
  167.  
  168.     p = begin;
  169.     while (p->next != NULL)
  170.     {
  171.         if (!strcmp(p->next->word, p->word))
  172.         {
  173.             q = p->next->next;
  174.             free(p->next);
  175.             p->next = q;
  176.         }
  177.         else
  178.             p = p->next;
  179.     }
  180. }
  181.  
  182.  
  183.  
  184. /***************
  185.  do_optimize()
  186.  sorts file `inf' to file `of'. If asc==0 sorting is done descending,
  187.  otherwise ascending.
  188. ***************/
  189.  
  190. void do_optimize(FILE *inf, FILE *of, char asc)
  191. {
  192.   struct words *begin;
  193.  
  194.   sort(inf, &begin, asc);
  195.   remdouble(begin);
  196.   to_file(of, begin);
  197.   free_buf(begin);
  198. }
  199.  
  200.  
  201. /***************
  202.  main()
  203.  main function of optimizer.
  204. ***************/
  205.  
  206. main (char argc, char **argv)
  207. {
  208.   char fname[80], oname[80];
  209.   char ascending=1;
  210.   int i;
  211.  
  212.   strcpy(fname, "");
  213.   strcpy(oname, "");
  214.  
  215.   if (argc > 1)
  216.   {
  217.     for (i=1; i<argc; i++)
  218.     {
  219.       switch(argv[i][0])
  220.       {
  221.       case '-': switch(toupper(argv[i][1]))
  222.           {
  223.             case 'F' : if (strlen(argv[i]) > 2)
  224.                           strcpy(fname, &argv[i][2]);
  225.                         else if (argc < (i+2))
  226.                           haltusage();
  227.                         else
  228.                           strcpy(fname, argv[++i]);
  229.                         break;
  230.             case 'O' : if (strlen(argv[i]) > 2)
  231.                           strcpy(oname, &argv[i][2]);
  232.                         else if (argc < (i+2))
  233.                            haltusage();
  234.                         else
  235.                            strcpy(oname, argv[++i]);
  236.                         break;
  237.             case 'R' : ascending = 0; break;
  238.             default  : haltusage();
  239.           }
  240.           break;
  241.       default : haltusage();
  242.       }
  243.     }
  244.   }
  245.  
  246.   if (strcmp(fname,""))
  247.     infile = fopen(fname, "rt");
  248.   else
  249.     infile = stdin;
  250.  
  251.   if (!infile)
  252.     {
  253.     fprintf(stderr, "optimize: %s: couldn't open file\n", fname);
  254.     exit(0);
  255.     }
  256.  
  257.   if (strcmp(oname, ""))
  258.     outfile = fopen(oname, "wt");
  259.   else
  260.     outfile = stdout;
  261.  
  262.   if (!outfile)
  263.     {
  264.     fprintf(stderr, "optimize: %s: could't create file\n", oname);
  265.     exit(0);
  266.     }
  267.  
  268.   do_optimize(infile, outfile, ascending);
  269.  
  270.   fclose(infile);
  271.   fclose(outfile);
  272.  
  273.   }
  274.  
  275. /* EOF OPTIMIZE.C */
  276.  
  277.